package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //0.参数判空
        if (wmSensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.获取敏感词
        String sensitives = wmSensitive.getSensitives();
        //2.查重
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(sensitives),WmSensitive::getSensitives,sensitives);
        Integer selectCount = wmSensitiveMapper.selectCount(wrapper);
        if(selectCount>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词重复");
        }
        //3.创建新的敏感词
        int insertResult = wmSensitiveMapper.insert(wmSensitive);
        if(insertResult<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"新增敏感词失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public PageResponseResult findSensitive(SensitiveDto sensitiveDto) {
        //0.参数判空
        if (sensitiveDto==null) {
            return new PageResponseResult(0,0,0);
        }
        //1.入参判空
        sensitiveDto.checkParam();
        Integer page = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();

        //2.敏感词模糊查询(时间倒序)
        String sensitive = sensitiveDto.getName();
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(sensitive),WmSensitive::getSensitives,sensitive);
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        //3.构建分页构造器
        Page<WmSensitive> pageInfo = new Page<>(page, size);
        Page<WmSensitive> pageResult = wmSensitiveMapper.selectPage(pageInfo, wrapper);
        //4.组装并返回
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page,size,total.intValue());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        //0.参数判空
        if (wmSensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.判断重名
        String sensitives = wmSensitive.getSensitives();
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(sensitives),WmSensitive::getSensitives,sensitives);
        Integer integer = wmSensitiveMapper.selectCount(wrapper);
        if(integer>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词存在");
        }

        //2.修改并返回
        int updateResult = wmSensitiveMapper.updateById(wmSensitive);
        if(updateResult<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"敏感词修改失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //0.参数判空
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.删除
        int deleteResult = wmSensitiveMapper.deleteById(id);
        if(deleteResult<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"敏感词删除失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
